from itertools import permutations
from itertools import combinations
from itertools import combinations_with_replacement

# 排列组合的迭代，想迭代遍历一个集合中元素的所有可能的排列或组合
def demo_1():
    items = ['a', 'b', 'c', 'd']
    for p in permutations(items):
        print(p)

def demo_2():
    items = ['a', 'b', 'c', 'd']
    # 设定排列的选择长度
    for p in permutations(items, 2):
        print(p)

# 遍历集合中元素的所有组合
def demo_3():
    items = ['a', 'b', 'c', 'd']
    for c in combinations(items, 3):
        print(c)
    for c in combinations(items, 2):
        print(c)

def demo_4():
    items = ['a', 'b', 'c', 'd']
    # combinations_with_replacement函数允许同一个元素被选择多次
    for c in combinations_with_replacement(items, 3):
        print(c)

if __name__ == '__main__':
    # demo_1()
    # demo_2()
    # demo_3()
    demo_4()